package com.lx.service.impl;

import com.lx.dao.BankCardDao;
import com.lx.dao.impl.BankCardDaoImpl;
import com.lx.entity.BankCard;
import com.lx.entity.Trade;
import com.lx.entity.vo.BankCardAndUserInfo;
import com.lx.entity.vo.TradeAndCardInfoVO;
import com.lx.service.BankCardService;
import com.lx.uitl.ConnUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @title: BankCardServiceImpl
 * @projectName bankManage
 * @description: TODO
 * @author: yuanChu
 * @date: 2022/5/6  8:34
 */
public class BankCardServiceImpl implements BankCardService {
    private BankCardDao bankCardDao = new BankCardDaoImpl();

    @Override
    public boolean addBankCard(BankCard bankCard) {
        Integer i = 0;
        try {
            i = bankCardDao.addBankCard(bankCard);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnUtil.closeConnection();
        }
        if (i != 0 && i != null) {
            return true;
        }
        return false;
    }


    @Override
    public int findCardCountById(String cardId) {
        try {
            return bankCardDao.findCardCountById(cardId);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("sql查询出现异常");
            return -1;
        }

    }

    @Override
    public BankCardAndUserInfo findCardInfoById(String cardId) {
        try {
            return bankCardDao.findCardInfoById(cardId);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("sql查询出现异常");
            return null;
        }
    }

    @Override
    public List<BankCard> findCardsByUserId(Integer userId) {
        try {
            return bankCardDao.findCardsByUserId(userId);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("sql查询出现异常");
            return null;
        }
    }

    @Override
    public boolean transfer(Trade trade) {
        try {
            Connection connection = ConnUtil.getConnection();
            connection.setAutoCommit(false);
            //扣款
            boolean chargeFlag = bankCardDao.charge(trade.getCardId(), trade.getTransMoney());
            if (!chargeFlag) {
                connection.rollback();
            }
            //加款
            boolean addFlag = bankCardDao.addMoney(trade.getPayeeCard(), trade.getTransMoney());
            if (!addFlag) {
                connection.rollback();
            }
            //生成转账记录
            boolean recordFlag = bankCardDao.insert(trade);
            if (!recordFlag) {
                connection.rollback();
            }
            //提交事务
            connection.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("sql查询出现异常");
            return false;
        } finally {
            ConnUtil.closeConnection();
        }
    }

    @Override
    public boolean deposit(double depositNum, BankCard bankCard) {
        try {
            Connection connection = ConnUtil.getConnection();
            connection.setAutoCommit(false);
            //加款
            boolean addFlag = bankCardDao.addMoney(bankCard.getCardId(), depositNum);
            if (!addFlag) {
                connection.rollback();
            }
            //生成存款记录
            Trade trade = new Trade();
            trade.setRemark("存款");
            trade.setTransTime(new Date());
            trade.setTransMoney(depositNum);
            trade.setBusinessType(1);
            trade.setCardId(bankCard.getCardId());
            boolean recordFlag = bankCardDao.deposit(trade);
            if (!recordFlag) {
                connection.rollback();
            }
            //提交事务
            connection.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("sql查询出现异常");
            return false;
        } finally {
            ConnUtil.closeConnection();
        }
    }

    @Override
    public boolean withdrawal(double withdrawal, BankCard card) {
        try {
            Connection connection = ConnUtil.getConnection();
            connection.setAutoCommit(false);

            //减款
            boolean addFlag = bankCardDao.withdrawal(card.getCardId(), withdrawal);
            if (!addFlag) {
                connection.rollback();
            }
            //生成扣款记录
            //减款的金额为负数
            withdrawal = withdrawal - withdrawal * 2;
            Trade trade = new Trade();
            trade.setRemark("取款");
            trade.setTransTime(new Date());
            trade.setTransMoney(withdrawal);
            trade.setBusinessType(1);
            trade.setCardId(card.getCardId());
            boolean recordFlag = bankCardDao.deposit(trade);
            if (!recordFlag) {
                connection.rollback();
            }
            //提交事务
            connection.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("sql查询出现异常");
            return false;
        } finally {
            ConnUtil.closeConnection();
        }
    }

    @Override
    public boolean updateStatusByCardId(String cardId) {
        Integer i = null;
        try {
            i = bankCardDao.updateStatusByCardId(cardId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnUtil.closeConnection();
        }
        if (i != null && i != 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<BankCard> findBalanceById(String cardId) {
        List<BankCard> list = null;
        try {
            list = bankCardDao.findBalanceById(cardId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnUtil.closeConnection();
        }
        return list;
    }

    @Override
    public boolean freeze(String cardId) {
        Integer i = 0;
        try {
            i = bankCardDao.freeze(cardId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnUtil.closeConnection();
        }
        if (i != 0 && i != null) {
            return true;
        }
        return false;
    }

    @Override
    public List<TradeAndCardInfoVO> getAccountLastMonthRecord(Integer userId) {
        try {
            return bankCardDao.getAccountLastMonthRecord(userId);

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            ConnUtil.closeConnection();
        }
    }

    /**
     * 查询某张卡的交易信息
     *
     * @param cardNo 卡号
     * @return 查询某张卡的交易信息
     */
    @Override
    public List<TradeAndCardInfoVO> selectLastMonthRecordByCard(String cardNo) {
        try {
            return bankCardDao.selectLastMonthRecordByCard(cardNo);

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            ConnUtil.closeConnection();
        }
    }
}
